﻿// -----------------------------------------------------------------------
// <copyright file="LivePreviewDialog.cs" company="FCIS">
// You are welcome to do whatever you want with the code in this file
// </copyright>
// -----------------------------------------------------------------------

namespace ImagePackage.WindowsForms
{
    using System;
    using System.ComponentModel;
    using System.Windows.Forms;

    /// <summary>
    /// A Dialog that gives live updates to the background image 
    /// </summary>
    public partial class LivePreviewDialog : Form, INotifyPropertyChanged
    {
        /// <summary>
        /// Livepreview controls
        /// </summary>
        private LivePreviewControl[] controls;

        /// <summary>
        /// Initializes a new instance of the <see cref="LivePreviewDialog"/> class.
        /// </summary>
        /// <param name="livePreviewInfo">The live preview info.</param>
        public LivePreviewDialog(params LivePreviewInfo[] livePreviewInfo)
        {
            this.InitializeComponent();
            if (livePreviewInfo == null)
            {
                throw new ArgumentNullException("livePreviewInfo");
            }

            if (livePreviewInfo.Length < 1)
            {
                throw new ArgumentException("Should supply at least one livePreviewInfo", "livePreviewInfo");
            }

            this.controls = new LivePreviewControl[livePreviewInfo.Length];
            for (int i = 0; i < livePreviewInfo.Length; i++)
            {
                this.controls[i] = new LivePreviewControl(livePreviewInfo[i]) { Location = new System.Drawing.Point(0, i * 50) };

                this.controls[i].PropertyChanged += this.OnPropertyChanged;
                this.Controls.Add(this.controls[i]);
            }
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="propertyLabel">The property label.</param>
        /// <returns>The value of the trackbar associated with the label given.</returns>
        internal int GetValue(string propertyLabel)
        {
            foreach (LivePreviewControl livePreviewControl in this.controls)
            {
                if (livePreviewControl.Info.Label == propertyLabel)
                {
                    return livePreviewControl.Value;
                }
            }

            throw new Exception(propertyLabel + " is not found");
        }

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="name">The name.</param>
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.OnPropertyChanged(e.PropertyName);
        }
    }
}
